package br.ufrj.dcc.smarthotel.activities;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import br.ufrj.dcc.smarthotel.R;
import br.ufrj.dcc.smarthotel.Util;
import br.ufrj.dcc.smarthotel.controls.RequestDetailControl;
import br.ufrj.dcc.smarthotel.entities.Request;
import br.ufrj.dcc.smarthotel.entities.RequestItem;
import br.ufrj.dcc.smarthotel.listeners.RequestClickListener;

public class RequestDetailActivity extends AppActivity {
	private RequestDetailControl control;
	private RequestClickListener listener;
	
	private Request clickedRequest;
	
	private TextView txtService;
	private TextView txtRequestDate;
	private TextView txtRegionName;
	private ListView listRequestItems;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        int clickedRequestId = Integer.parseInt(this.getIntent().getExtras().getString("clickedRequestId"));
        
        setContentView(R.layout.requestdetail);
        
        this.findAllViews();
        this.control = new RequestDetailControl(this);
        this.listener = new RequestClickListener(this);
        
        this.control.loadRequestDetails(clickedRequestId);
    }

	private void findAllViews() {
		this.txtRegionName = (TextView)this.findViewById(R.id.txtRegionName);
		this.txtService = (TextView)this.findViewById(R.id.txtService);
		this.txtRequestDate = (TextView)this.findViewById(R.id.txtRequestDate);
		this.listRequestItems = (ListView)this.findViewById(R.id.listRequestItems);
	}

	public void setClickedRequest(Request clickedRequest) {
		this.clickedRequest = clickedRequest;
		
		this.txtRegionName.setText(this.clickedRequest.getRegionName());
		this.txtRequestDate.setText(Util.getInstance(this.getApplicationContext()).writeDate(this.clickedRequest.getRequestDate()));
		this.txtService.setText(this.clickedRequest.getService().getName());
		
		this.listRequestItems.setAdapter(new RequestItemsListAdapter(this.getApplicationContext(), R.id.listRequestItems, this.clickedRequest.getRequestItemList()));
	}
	
	class RequestItemsListAdapter extends ArrayAdapter<RequestItem> {
		List<RequestItem> requestItemsList;
		
		public RequestItemsListAdapter(Context context, int textViewResourceId, List<RequestItem> objects) {
			super(context, textViewResourceId, objects);
			
			this.requestItemsList = objects;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			
	        if (v == null) {
	            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            
	            v = vi.inflate(R.layout.eventitem_list, null);
	        }
	        
	        RequestItem requestItem = this.requestItemsList.get(position);
	        
	        if (requestItem != null) {
                TextView txtDescription = (TextView) v.findViewById(R.id.txtDescription);
                TextView txtQuantity = (TextView) v.findViewById(R.id.txtQuantity);
                
                txtDescription.setText(requestItem.getDescription());
                txtQuantity.setText(requestItem.getQuantity());
	        }
	        return v;
		}	
	}
}
